using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Excel;

namespace Excel2XPS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Write to the log that MOSS passes in.  
            //Generally C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS
            ParseCommandLine(args);
            gLog = new StreamWriter(gLogPath, true);

            try
            {
                gLog.WriteLine(DateTime.Now.ToString() + " Started");
                gLog.WriteLine(Environment.CommandLine);
               
                ConvertDoc2XPS(gInFileName, gOutFileName);
            }
            catch (Exception e)
            {
                gLog.WriteLine(e.Message);
                Environment.ExitCode = 100;
            }
            finally
            {
                gLog.Close();
            }
        }

        private static void ParseCommandLine(string[] args)
        {
            int i = 0;
            while (i < args.Length)
            {
                string s = args[i];
                switch (s.ToLower())
                {
                    case "-in":
                        i++;
                        gInFileName = args[i];
                        break;
                    case "-out":
                        i++;
                        gOutFileName = args[i];
                        break;
                    case "-config":
                        // Skip the name of the config file and do nothing.
                        i++;
                        break;
                    case "-log":
                        i++;
                        gLogPath = args[i];
                        break;
                    default:
                        throw new Exception("Unknown command line argument: " + s);
                }
                i++;
            }
        }
        private static void ConvertDoc2XPS(string inFileName, string outFileName)
        {
            ApplicationClass excelApplication = new ApplicationClass();
            Workbook excelWorkBook = null;

            string paramSourceBookPath = inFileName;
            object paramMissing = Type.Missing;


            string paramExportFilePath = outFileName;
            XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypeXPS;
            XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
            bool paramOpenAfterPublish = false;
            bool paramIncludeDocProps = true;
            bool paramIgnorePrintAreas = true;
            object paramFromPage = Type.Missing;
            object paramToPage = Type.Missing;

            try
            {
                // Open the source workbook.
                excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath,
                    paramMissing, paramMissing, paramMissing, paramMissing,
                    paramMissing, paramMissing, paramMissing, paramMissing,
                    paramMissing, paramMissing, paramMissing, paramMissing,
                    paramMissing, paramMissing);

                // Save it in the target format.
                if (excelWorkBook != null)
                    excelWorkBook.ExportAsFixedFormat(paramExportFormat,
                        paramExportFilePath, paramExportQuality,
                        paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
                        paramToPage, paramOpenAfterPublish,
                        paramMissing);
            }
            catch (Exception e)
            {
                gLog.WriteLine(e.Message);
                Environment.ExitCode = 100;
            }
            finally
            {
                // Close the workbook object.
                if (excelWorkBook != null)
                {
                    excelWorkBook.Close(false, paramMissing, paramMissing);
                    excelWorkBook = null;
                }

                // Quit Excel and release the ApplicationClass object.
                if (excelApplication != null)
                {
                    excelApplication.Quit();
                    excelApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            } 
        }

        private static string gInFileName;
        private static string gOutFileName;
        private static string gLogPath;
        private static StreamWriter gLog;
    }
}
